<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<link rel="stylesheet" type="text/css" href="../css/common.css" media="all" />
<link rel="stylesheet" type="text/css" href="../css/article.css" media="all" />
</head>
<body>
<div id="w3h_body">
  <div class="body_content">
    <!-- toc begin -->
    <h1 class="title">BT9017: 只有 IE 支持 mouseenter 和 mouseleave 事件</h1>
    <ul class="toc">
      <li><a href="#standard_reference">标准参考</a> <span>•</span></li>
      <li><a href="#description">问题描述</a> <span>•</span></li>
      <li><a href="#influence">造成的影响</a> <span>•</span></li>
      <li><a href="#impacted_browsers">受影响的浏览器</a> <span>•</span></li>
      <li><a href="#analysis_of_issues">问题分析</a> <span>•</span></li>
      <li><a href="#solutions">解决方案</a> <span>•</span></li>
      <li><a href="#see_also">参见</a></li>
    </ul>
    <!-- toc end -->
    <div id="w3h_content">
      <!-- content begin -->
      <address class="author">作者：蔡美纯 钱宝坤</address>
      <h2 id="standard_reference">标准参考</h2>
      <p>无</p>

      <h2 id="description">问题描述</h2>
      <p>在 IE 中，用户将鼠标移入移出某对象时，触发 mouseenter 和 mouseleave 事件。<br />
      此事件不会冒泡。</p>

      <h2 id="influence">造成的影响</h2>
      <p>只有 IE6 IE7 IE8 浏览器支持 mouseente 和 mouseleave 事件。</p>

      <h2 id="impacted_browsers">受影响的浏览器</h2>
      <table class="list">
        <tr>
          <th>Firefox Safari Chrome Opera</th>
          <td></td>
        </tr>
      </table>

      <h2 id="analysis_of_issues">问题分析</h2>
      <p>mouseenter/mouseleave 事件只有 IE 支持。以下为 <a href="http://msdn.microsoft.com/en-us/library/ms536945%28VS.85%29.aspx">MSDN</a> 对 mouseenter 的具体解释：</p>
      <p>The event fires only if the mouse pointer is outside the boundaries of the object and the user moves the mouse pointer inside the boundaries of the object. If the mouse pointer is currently inside the boundaries of the object, for the event to fire, the user must move the mouse pointer outside the boundaries of the object and then back inside the boundaries of the object.</p>
      <p>Unlike the onmouseover event, the onmouseenter event does not bubble. In other words, the onmouseenter event does not fire when the user moves the mouse pointer over elements contained by the object, whereas onmouseover does fire.</p>
      <p>示例代码</p>
<pre>
&lt;p id="a"&gt;...&lt;/p&gt;
&lt;script&gt;
document.getElementById('a').onmouseenter=function(){this.innerHTML="mouseenter fired"}
document.getElementById('a').onmouseleave=function(){this.innerHTML="mouseleave fired"}
&lt;/script&gt;
</pre>
      <p>将鼠标移到 P 元素内，仅在 IE6 IE7 IE8 中，P 元素内的文字会改变。</p>
      <p>相比 DOM 规范中的 mouseout 和 mouseover 连个事件 IE 的 mouseenter 和 mouseleave 事件不会在鼠标移入移出容器内其他子标记时触发相应事件。</p>
      <p>具体实例可以参见下方的测试页面——当鼠标移入灰色子元素时会触发mouseout 和 mouseover 事件，但是 IE 专有事件模型则没有这个问题。</p>
      
      <h2 id="solutions">解决方案</h2>
      <p>1. 避免使用 mouseenter 和 mouseleave 事件。<br/>
      2. 在非 IE 浏览器中模拟这两个事件。如：<em>res.html</em></p>
      <pre>&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;head&gt;
&lt;script&gt;
  window.onload = function (){
    var ua = navigator.userAgent;
    Test = {
      version: (ua.match(/.+(?:rv|it|ra|ie|me)[\/: ]([\d.]+)/i)||[])[1],
      ie: /msie/i.test(ua) &amp;&amp; !/opera/i.test(ua),
      op: /opera/i.test(ua),
      sa: /version.*safari/i.test(ua),
      ch: /chrome/.test(ua),
      ff: /gecko/i.test(ua) &amp;&amp; !/webkit/i.test(ua),
      wk: /webkit/i.test(ua),
      mz: /mozilla/i.test(ua)&amp;&amp;!/(compatible|webkit)/i.test(ua)
    }
    
    function $(id) { return document.getElementById(id); }
    
    function addEvent(el, type, fn){
      (el.attachEvent) ? (el.attachEvent("on" + type, fn)) : (el.addEventListener(type, fn, false));
    };

    var contains = document.compareDocumentPosition
      ? function (parent, child) {
        return !!(parent.compareDocumentPosition(child) &amp; 16);
      }
      : function (parent, child) {
        return child !== child &amp;&amp; (parent.contains ? parent.contains(child) : true);
      }

    function fixMouseLeave (elem, fn){
      var mouseleave = Test.ie ? "mouseleave" : "mouseout";
      (elem == null || elem == window ) &amp;&amp; (elem = document);
      return {
        type: mouseleave,
        elem: elem,
        fn: Test.ie ? fn : function(e) {
          (contains(e.relatedTarget,this)) &amp;&amp; (fn.call(this, e));
        }
      }
    }
    
    function fixMouseEnter (elem, fn){
      var mouseenter = Test.ie ? "mouseenter" : "mouseover";
      (elem==null || elem==window ) &amp;&amp; (elem = document);
      return {
        type: mouseenter,
        elem: elem,
        fn: Test.ie ? fn : function(e) {
          (contains(e.relatedTarget,this)) &amp;&amp; (fn.call(this ,e));
        }
      }
    }
    
    var me = fixMouseEnter($("a"), function (e) {
      $("b").value += "enter\n";
    });
    var ml = fixMouseLeave($("a"), function (e) {
      $("b").value += "leave\n";
    });
    
    addEvent(me.elem, me.type, me.fn);
    addEvent(ml.elem, ml.type, ml.fn);
  }
&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
  &lt;div id="a" style="background:gold; width:100px; height:100px; padding:30px"&gt;
    &lt;div style="background:gray; height:100%;"&gt;&lt;/div&gt;
  &lt;/div&gt;
  &lt;textarea id="b" style="width:100px; height:100px;"&gt;&lt;/textarea&gt;
&lt;/body&gt;
&lt;/html&gt;</pre>

      <h2 id="see_also">参见</h2>
      <h3>知识库</h3>
      <ul class="see_also">
        <li><a href="#">...</a></li>
      </ul>

      <h3>相关问题</h3>
      <ul class="see_also">
        <li><a href="#">...</a></li>
      </ul>

      <div class="appendix">
        <h2>测试环境</h2>
        <table class="list">
          <tr>
            <th>操作系统版本:</th>
            <td>Windows 7 Ultimate build 7600</td>
          </tr>
          <tr>
            <th>浏览器版本:</th>
            <td>
              IE6<br />
              IE7<br />
              IE8<br />
              Firefox 3.6<br />
              Chrome 4.0.302.3 dev<br />
              Safari 4.0.4 <br />
              Opera 10.60
            </td>
          </tr>
          <tr>
            <th>测试页面:</th>
            <td><a href="../../tests/BT9017/mouseenter_and_mouseleave.html">mouseenter_and_mouseleave.html</a><br />
            <a href="../../tests/BT9017/res.html">res.html</a></td>
          </tr>
          <tr>
            <th>本文更新时间:</th>
            <td>2010-08-13</td>
          </tr>
        </table>

        <h2>关键字</h2>  
        <!-- keywords begin -->
        <p>mouseenter mouseleave</p>
        <!-- keywords end -->
      </div>
      <!-- content end -->
    </div>
  </div>
</div>
</body>
</html>
